package com.ludomancy.spaceopera;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.Rectangle;

import com.ludomancy.gamelib.ImageStrip;
import com.ludomancy.gamelib.ResourceManager;
import com.ludomancy.gamelib.Sound;
import com.ludomancy.gamelib.Utils;


public class StationButton extends BasicEntity
{
	private ImageStrip sprites;
	private Image buttonBack;
	private InventoryItem item;
	
	public StationButton(InventoryItem item)
	{
		super(new Point());
		buttonBack = ResourceManager.getImage("buttonback.png");
		sprites = new ImageStrip(ResourceManager.getImage("buttons.png"), 20, 24);
		width = (int) proj.screenToWorldX(buttonBack.getWidth(null));
		height = (int) proj.screenToWorldY(buttonBack.getHeight(null));
		
		// init item
		this.item = item;
	}
	
	public int getCost()
	{
		if(item == null)
		{
			return 0;
		}
		else
		{
			return item.getCost();
		}
	}
	
	public String getInteractionVerb()
	{
		if(item != null)
		{
			return item.getBuyLabel();
		}
		return "";
	}
	
	public String getDescription()
	{
		if(item == null)
		{
			return "";
		}
		String desc = item.getDescription();
		if(!canAfford())
		{
			desc += "\n*Not enough Resources*";
		}
		return desc;
	}
	
	public boolean isInteractable()
	{
		return item != null;
	}
	
	public void paint(Graphics g, int x, int y)
	{
		g.drawImage(buttonBack, x, y, null);
		if(item != null)
		{
			Point p = Utils.getCenterInRect(new Point(sprites.frameWidth, sprites.frameHeight), new Rectangle(x,y,buttonBack.getWidth(null), buttonBack.getHeight(null)));
			if(canAfford())
			{
				sprites.paint(g, item.getType(), p.x, p.y);
			}
			else
			{
				sprites.paint(g, InventoryItem.TYPE_CLASSES+item.getType(), p.x, p.y);
			}
		}
	}
	
	public boolean canAfford()
	{
		return item != null && Game.game.player.getCargo() >= item.getCost();
	}
	
	public void onPlayerInteraction(Player player)
	{
		if(item == null)
		{
			return;
		}
			
		if(canAfford())
		{
			player.pay(item.getCost());
			item.grant();
			Game.game.station.removeItem(item);
			item = null;
			Sound.play("install.wav");
		}
	}
}
